<?php
/**
* Adds and store the custom meta values for the page.
* 
* With following Features
* added June 23rd 2012
* 
* Project Map on full page
* Project Status. (coming soon/Complete/Presale)
* Project address (For Google map)
* project url     (For Visit Site)
* Project Image   (featured image)
* Project Description   (Paragarph about the project)
*/



/* Adds a box to the main column on the Post and Page edit screens */
function cfa_royalgroup_page_add_custom_box() {
    add_meta_box(  'royalgroup_sectionid', __( 'Project Options (only fill in data for Project Pages)', 'CFAStudio' ), 'cfa_royalgroup_page_inner_custom_box', 'page' );
}
add_action( 'add_meta_boxes', 'cfa_royalgroup_page_add_custom_box' );

/* Prints the box content */
function cfa_royalgroup_page_inner_custom_box( $post ) {
    
    $project_status      = get_post_meta($post->ID, 'project_status', true);
    $project_address     = get_post_meta($post->ID, 'project_address', true);
    $project_url         = get_post_meta($post->ID, 'project_url', true);
    $project_description = get_post_meta($post->ID, 'project_description', true);
    
    $values = array(
		'values'  => array('COMING'   => 'Coming Soon', 
		                   'PRESALE'  => 'Pre Sale',
						   'COMPLETE' => 'Completed'),
		'name'    => 'project_status',
		'selected'=> $project_status,
    );

    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'cfa_project_page_nonce' );
?>
<div id="postcustomstuff">
<table>
    <tr>
        <td class="left">
            <label ><?php echo  _e("Project Status", 'CFAStudio' ); ?></label> <br />
        <?php generate_dropdown($values); ?>
        </td>
    </tr>

    <tr>
        <td class="left">
            <label ><?php echo  _e("Project Address (GOOGLE MAP)", 'CFAStudio' ); ?></label><br />
            Address used in google map marker
            
            <input type="text" name="project_address" value="<?php echo @$project_address ?>">
        </td>
    </tr>
    
    <tr>
        <td class="left">
            <label ><?php echo  _e("Project URL", 'CFAStudio' ); ?></label> <br />
            <input type="text" name="project_url" value="<?php echo @$project_url ?>">
        </td>
    </tr>
    
    <tr>
        <td class="left">
            <label ><?php echo  _e("Project Description", 'CFAStudio' ); ?></label> <br />
            <textarea name="project_description" rows="10" cols="60"><?php echo @$project_description ?></textarea>
        </td>
    </tr>

</table>
</div>
<?php
  
}

/* When the post is saved, saves our custom data */
function cfa_royalgroup_page_save_postdata( $post_id ) {
  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times

  if ( !wp_verify_nonce( $_POST['cfa_project_page_nonce'], plugin_basename( __FILE__ ) ) )
      return;

    update_post_meta( $post_id, 'project_status'      , $_POST[ 'project_status' ]);
    update_post_meta( $post_id, 'project_address'     , $_POST[ 'project_address' ]);
	
    update_post_meta( $post_id, 'project_url'         , $_POST[ 'project_url' ]);
    update_post_meta( $post_id, 'project_description' , $_POST[ 'project_description' ]);
	
	
	update_post_meta( $post_id, 'bgmp_address'        , $_POST[ 'project_address' ] );
	
	
	if( $_POST[ 'project_address' ] ) {
		$map = new BasicGoogleMapsPlacemarks();
		$coordinates = $map->geocode( $_POST[ 'project_address' ] );
	}
		
	if( $coordinates )
	{
		update_post_meta( $post_id, 'bgmp_' . 'latitude', $coordinates[ 'latitude' ] );
		update_post_meta( $post_id, 'bgmp_' . 'longitude', $coordinates[ 'longitude' ] );
	}
	else
	{	
		update_post_meta( $post_id, 'bgmp_' . 'latitude', '' );
		update_post_meta( $post_id, 'bgmp_' . 'longitude', '' );
	}
}

/* Do something with the data entered */
add_action( 'save_post', 'cfa_royalgroup_page_save_postdata' );

/* shows the map of the page */
function get_page_show_map($attr) {
    global $post;
    
    $center = trim(get_post_meta($post->ID, 'project_address', true));
	
	if ( empty($center) ) return;
    
    $map .= '<a href="https://maps.google.com/maps?q='.$center.'">';
    $map .= '<img class="imageborder" src="http://maps.googleapis.com/maps/api/staticmap?center='.$center;
    $map .= '&zoom=13&size=390x295';
    $map .= '&sensor=false';
//    $map .= '&maptype=roadmap&markers=color:blue%7Clabel:S%7C40.702147,-74.015794';
    $map .= '&maptype=roadmap';
    $map .= '&markers=color:blue%7Clabel:S%7C'.$center;
    $map .= '" />';
    $map .= '</a>';

    return $map;
    
}
add_shortcode( 'page_show_map', 'get_page_show_map' );
